package edu.ucsc.mit2008.swan.core;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;

import edu.ucsc.mit2008.swan.core.internal.menu.EditorMenuExtension;
import edu.ucsc.mit2008.swan.core.internal.menu.EditorMenuExtension.EditorMenuItem;
import edu.ucsc.mit2008.swan.core.util.ResourceUtils;

/**
 * Main window for the Swan Editor.
 * 
 * @author Thushara Sanjeewa Ramasinghe
 *
 */
public final class SwanWindow extends ApplicationWindow {

	private List<EditorMenuExtension> menuExtentions = new ArrayList<EditorMenuExtension>();
	
	public SwanWindow(Shell parentShell) {
		super(parentShell);
	}

	@Override
	protected Control createContents(Composite parent) {
	    getShell().setText("Swan");
	    ImageDescriptor descriptor = ResourceUtils.createImageDescriptor(new Path("/icons/swanMain.gif"), true);
	    getShell().setImage(descriptor.createImage());
	    parent.setSize(800,500);
	    createMenu(getShell());
		return super.createContents(parent);
	}

	protected void createMenu(Shell shell){		
		// loads the menu extentions
		loadMenuExtentions();
		
	    // Create the bar menu
	    Menu menuBar = new Menu(shell, SWT.BAR);

	    // Create all the items in the bar menu
	    for(EditorMenuExtension editorMenuExtension : menuExtentions){
		    // Create the File item's dropdown menu
		    Menu fileMenu = new Menu(menuBar);
		    MenuItem fileItem = new MenuItem(menuBar, SWT.CASCADE);
		    fileItem.setText(editorMenuExtension.getMenuName());
		    
		    for(final EditorMenuItem editorMenuItem :editorMenuExtension.getMenuItems()){
		    	MenuItem item = new MenuItem(fileMenu, SWT.PUSH);
		    	item.setText(editorMenuItem.getName());
		    	item.addSelectionListener(new SelectionAdapter() {

					@Override
					public void widgetSelected(SelectionEvent e) {
						Action action = editorMenuItem.getAction();
						if(action != null){
							action.run();
						}
					}
		    		
				});
		    }
		    fileItem.setMenu(fileMenu);	    	
		    
	    }	    
	    shell.setMenuBar(menuBar);
	}
	
	/**
	 * Loads the menu extentions.
	 * 
	 */
	private void loadMenuExtentions(){
    	IExtension[] exts =
    	    org.eclipse.core.runtime.Platform.getExtensionRegistry()
    	        .getExtensionPoint(ISwanConstants.EDITOR_MENU_EXTENTION_ID).getExtensions();
    	for (IExtension extension : exts) {
    		for (IConfigurationElement element : extension.getConfigurationElements()) {
    			EditorMenuExtension menuExtension = new EditorMenuExtension(element);
    			menuExtentions.add(menuExtension);
    		}
    	}
	}
}
